package com.system.healthproject.model.admin.controller;

import com.system.healthproject.bean.*;
import com.system.healthproject.common.ApiResult;
import com.system.healthproject.model.admin.service.DoctorService;
import com.system.healthproject.pojo.Position;
import com.system.healthproject.utils.SimpleFormatutiles;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.constraints.NotBlank;
import javax.xml.bind.SchemaOutputResolver;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 医生
 */
@Controller
@Api(value = "后端-医生管理" ,tags = "后端-医生数据接口api")
@ApiResponses({
        @ApiResponse(code=400,message="请求参数不完整或者错误"),
        @ApiResponse(code=404,message="找不到页面"),
        @ApiResponse(code=415,message="请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串"),
        @ApiResponse(code=500,message="后台服务错误")
})
@RequestMapping("/admin/doctor")
public class DoctorControll {
    @Autowired
    private DoctorService doctorService;



    @RequestMapping(value = "/addDoctor" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "新增医生(后台已调用)",notes = "添加医生",httpMethod = "post",response = ApiResult.class)
    @ResponseBody
        public ApiResult addDoctor(@RequestBody @ApiParam(name = "doctor",value = "医生信息")ParameterDoctor parameterDoctor){
        String msg = doctorService.addDoctor(parameterDoctor);
        return new ApiResult(200,msg);
    }



 @RequestMapping(value = "/updateDoctor" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "修改医生信息(后台已调用)",notes = "修改医生信息",httpMethod = "post",response = ApiResult.class)
    @ResponseBody
 @ApiImplicitParams({
         @ApiImplicitParam(name = "doctorId",value = "医生ID",paramType = "query",dataType = "int",required = true),
         @ApiImplicitParam(name = "doctorName",value = "医生名字",paramType = "query",dataType = "String",required = false),
         @ApiImplicitParam(name = "doctorSex",value = "医生性别",paramType = "query",dataType = "int",required = false),
         @ApiImplicitParam(name = "hospitalId",value = "所属医院",paramType = "query",dataType = "int",required = false),
         @ApiImplicitParam(name = "deptId",value = "科室",paramType = "query",dataType = "int",required = false),
         @ApiImplicitParam(name = "posId",value = "职称",paramType = "query",dataType = "int",required = false),
         @ApiImplicitParam(name = "doctorExperience",value = "从医经验",paramType = "query",dataType = "String",required = false),
         @ApiImplicitParam(name = "doctorType",value = "医生类型",paramType = "query",dataType = "int",required = false),
         @ApiImplicitParam(name = "doctorIntroduce",value = "基本信息",paramType = "query",dataType = "String",required = false),
         @ApiImplicitParam(name = "doctorRole",value = "账号角色",paramType = "query",dataType = "int",required = false)
 })
        public ApiResult updateDoctor(@Validated ParamterUpdateDoctorInfo paramterUpdateDoctorInfo){
     System.out.println(paramterUpdateDoctorInfo.getDoctorName());
            String msg = doctorService.updateDoctor(paramterUpdateDoctorInfo);
        return new ApiResult(200,msg);
    }

    /**
     * 查看医生详细信息
     * @param
     * @return
     */
    @RequestMapping(value = "/selectByIdDoctor" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询医生详细信息(接口已调用)",notes = "查询医生详细信息",httpMethod = "post")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId",value = "医生ID",paramType = "query",dataType = "int",required = true)
    })
    public ApiResult<PagesBean<ResultDoctorInfo>> selectByIdDoctor(Integer doctorId){
        ResultDoctorInfo doctorInfo = doctorService.selectDoctorInformation(doctorId);
        return new ApiResult(200,"成功",doctorInfo);
    }

    /**
     * 查询所有职称ID、名称
     */
    @RequestMapping(value = "/selectPosition" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询所有职称ID、名称(接口已调用)",notes = "查询所有职称ID、名称",httpMethod = "post")
    @ResponseBody
    public ApiResult selectPosition( ){
         List<Position> list  = doctorService.selectPosition();
        return new ApiResult(200,"成功",list);
    }

/**
     * 医生编辑患者信息
     * @param
     * @return
     */
/*@RequestMapping(value = "/updateByIdPatient" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
@ApiOperation(value = "医生编辑患者信息",notes = "医生编辑患者信息",httpMethod = "post")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patientId",value = "患者ID",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "patientNick",value = "患者姓名",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "patientSex",value = "患者性别",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "province",value = "地址（省）",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "city",value = "地址（市）",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "lastVisit",value = "上一次就诊时间",paramType = "query",dataType = "date",required = false),
            @ApiImplicitParam(name = "fillInDegree",value = "病历填写程度（已填写  未填写）",paramType = "query",dataType = "String",required = false)
    })
    public ApiResult<PagesBean<ResultPatient>> updateByIdPatient(Integer doctorId){
        return new ApiResult<PagesBean<ResultPatient>>(200,"成功");
    }*/



    /**
     *   查询医生列表、条件查询
     * @param
     * @return
     */
    @RequestMapping(value = "/selectDoctor" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询医生（包括条件查询）/接口已调用",notes = "查询医生（包括条件查询）",httpMethod = "post")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorName",value = "医生名字",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "hospitalId",value = "医院ID",paramType = "query",dataType = "int",required = false),
            @ApiImplicitParam(name = "doctorStart",value = "账号状态（1 审核  2 未审核）",paramType = "query",dataType = "int",required = false),
            @ApiImplicitParam(name = "startTime",value = "创建时间（开始）",paramType = "query",dataType = "Date",required = false),
            @ApiImplicitParam(name = "endTime",value = "创建时间（结束）",paramType = "query",dataType = "Date",required = false),
            @ApiImplicitParam(name = "doctorSex",value = "医生性别(1、男  2、女  3、全部)" ,paramType = "query",dataType = "int",required = false),
            @ApiImplicitParam(name = "currentPage",value = "当前页",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "limit",value = "每页显示数据条数",paramType = "query",dataType = "int",required = true),
    })
    @ResponseBody
    public ApiResult<PagesBean<ResultDoctor>> selectDoctor(
            String doctorName,
            Integer hospitalId,
            Integer doctorStart,
            String startTime,
            String endTime,
            Integer doctorSex,
            Integer currentPage,
            Integer limit){
        System.out.println(doctorSex);
        List<ResultDoctor> list =doctorService.selectDoctor(doctorName,hospitalId,doctorStart,startTime,endTime,doctorSex,currentPage,limit);
        Integer count =doctorService.countDoctor(doctorName,hospitalId,doctorStart,startTime,endTime,doctorSex);
        PagesBean<ResultDoctor> pagesBean = new PagesBean<>();
        pagesBean.setData(list);
        pagesBean.setNowsPage(currentPage);
        pagesBean.setPageSize(limit);
        pagesBean.setDataCount(count);
        return new ApiResult<PagesBean<ResultDoctor>>(200,"成功",pagesBean);
    }




    /**
     *  导出为Excel表格
     * @param
     * @return
     */
    @RequestMapping(value = "/export" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "导出Excel表格",notes = "导出Excel表格",httpMethod = "post",response = ApiResult.class)
   /* @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorName",value = "医生名字",paramType = "query",dataType = "String",required = false),
            @ApiImplicitParam(name = "hospitalId",value = "医院ID",paramType = "query",dataType = "int",required = false),
            @ApiImplicitParam(name = "hospitalId",value = "账号状态（1 审核    2 未审核）",paramType = "query",dataType = "int",required = false),
            @ApiImplicitParam(name = "startTime",value = "创建时间（开始）",paramType = "query",dataType = "Date",required = false),
            @ApiImplicitParam(name = "endTime",value = "创建时间（结束）",paramType = "query",dataType = "Date",required = false),
            @ApiImplicitParam(name = "doctorSex;",value = "医生性别(1、男  2、女  3、全部)",paramType = "query",dataType = "int",required = false),
    })*/
    @ResponseBody
    public ApiResult export(){
        /*ResultDoctor doctorResult = new ResultDoctor();
        doctorResult.setDoctorName("张三");
        doctorResult.setDoctorSex("男");
        doctorResult.setDoctorExperience("10年");
        doctorResult.setHospitalName("所属医院名称");
        doctorResult.setPositionName("医生职称");
        doctorResult.setDepartmentName("医生所在科室");
        doctorResult.setLoginPhone("医生电话");
        doctorResult.setDoctorLicense("医生资格证");
        doctorResult.setCurrentPage(2);
        doctorResult.setLimit(10);
        doctorResult.setCountData(100);
*/
        return new ApiResult(200,"成功");
    }





    /**
     *   新增诊疗活动
     * @param
     * @return
     */
    @RequestMapping(value = "/addConsulting" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "新增诊疗活动(后端已调用)",notes = "新增诊疗活动",httpMethod = "post",response = ApiResult.class)
    @ResponseBody
    public ApiResult addConsulting(@RequestBody@ApiParam(name = "添加诊疗活动参数",value = "添加诊疗活动参数") ParamAddConsulting paramAddConsulting){
        System.out.println(paramAddConsulting);
        String msg = doctorService.addConsulting(paramAddConsulting);
        return new ApiResult(200,msg);
    }




    /**
     * 查看医生某一天诊疗活动详情
     * @param
     * @return
     */
    @RequestMapping(value = "/selectByDayConsulting" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查看医生某一天的诊疗活动(参加人数、地点)接口已调用",notes = "查看医生某一天的诊疗活动",httpMethod = "post")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId",value = "医生ID",paramType = "query",dataType = "int",required=true),
            @ApiImplicitParam(name = "date",value = "日期 yyyy-MM-dd",paramType = "query",dataType = "String",required = true),
    })
    public ApiResult<ResultDoctorConsulting> selectByDayConsulting(Integer doctorId,String date){
        List<ResultDoctorConsulting> doctorConsulting = doctorService.selectDoctorConsulting(doctorId,date);
        return new ApiResult(200,"成功",doctorConsulting);
    }


    /**
     * 查询医生待消核桃币(查询医生核桃币)
     * @param
     * @return
     */
    @RequestMapping(value = "/selectWalnutCoin" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "查询医生待消核桃币(接口已调用)",notes = "查询医生待消核桃币",httpMethod = "post",response = ApiResult.class)
    @ApiImplicitParam(name = "doctorId",value = "医生ID",paramType = "query",dataType = "int",required = true)
    @ResponseBody
    public ApiResult selectWalnutCoin(Integer doctorId){
        Double money = doctorService.selectSurplus(doctorId);
        return new ApiResult(200,"核桃币数量",money);
    }



    /**
     * 医生消核桃币
     * @param
     * @return
     */
    @RequestMapping(value = "/eliminateWalnutCoin" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId",value = "医生ID",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "walnutCoinNumber",value = "核桃币数量",paramType = "query",dataType = "date",required = true),
            @ApiImplicitParam(name = "remarks",value = "备注",paramType = "query",dataType = "String",required = false),
    })
    @ResponseBody
    @ApiOperation(value = "医生消除核桃币(后端已调用)",notes = "医生消除核桃币",httpMethod = "post",response = ApiResult.class)
    public ApiResult eliminateWalnutCoin(Integer doctorId,Double walnutCoinNumber,String remarks){
        String msg = doctorService.eliminate(doctorId,walnutCoinNumber,remarks);

        return new ApiResult(200,msg);
    }



    /**
     * 医生交易信息
     * @param
     * @return
     */
    @RequestMapping(value = "/doctorTransaction" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "limit",value = "每页显示数据条数",paramType = "query",dataType = "int",required = true),
    })
    @ApiOperation(value = "医生交易信息(后端已调用)",notes = "医生交易信息(接口已调用)",httpMethod = "post")
    @ResponseBody
    public ApiResult<PagesBean<ResultDoctorTransaction>> doctorTransaction(Integer currentPage,Integer limit){
            List<ResultDoctorTransaction> list = doctorService.selectDoctorTransaction(currentPage,limit);
            Integer count = doctorService.countDoctorTransaction();
       PagesBean<ResultDoctorTransaction> pagesBean = new PagesBean<>();
       pagesBean.setDataCount(count);
       pagesBean.setData(list);
       pagesBean.setNowsPage(currentPage);
       pagesBean.setPageSize(limit);
        return new ApiResult(200,"成功",pagesBean);
    }


    /**
     * 医生停用/启用
     * @param
     * @return
     */
    @RequestMapping(value = "/stopStartUp" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "医生停用/启用(接口已调用)",notes = "医生停用/启用",httpMethod = "post",response = ApiResult.class)
    @ApiImplicitParams({
           @ApiImplicitParam(name = "doctorId",value = "医生ID",paramType = "query",dataType = "int",required = true) ,
           @ApiImplicitParam(name = "state",value = "状态",paramType = "query",dataType = "int",required = true) ,
    })
    @ResponseBody
    public ApiResult stopStartUp(Integer doctorId,Integer state){
        String msg = doctorService.stopStartUp(doctorId,state);
        return new ApiResult(200,msg);
    }


    @ApiOperation(value = "查询医院和医生的信息接口(已调试)",notes = "查询医院和医生的信息接口",httpMethod = "post")
    @RequestMapping(value = "/selectHospitalAndDoctor",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public  ApiResult<List<HospitalDoctorResultBean>> selectHospitalAndDoctor (){

       List<HospitalDoctorResultBean> list=new ArrayList<>();

        list=doctorService.selectHospitalAndDoctor();

        ApiResult apiResult=new ApiResult();

        if (list.size() >0) {
            apiResult.setData(list);
            apiResult.setMsg("成功！");
            apiResult.setCode(200);
        }else {
            apiResult.setMsg("失败！");
            apiResult.setCode(500);
        }
        return apiResult;

    }
}
